from common_import import *


def merge_daily_data(day_index):
    """合并指定日期的所有商品数据到一个结构化数组中."""

    records = []
    dtype = [
        ("code", "i4"),
        ("quantity", "f4"),
        ("sales_price", "f4"),
        ("cost_price", "f4"),
    ]
    for code in mapping.focus_products:
        item_data = tool.get_np(f"product_forcast/{code}.csv")
        record = (
            code,
            item_data["quantity"][day_index],
            item_data["sales_price"][day_index],
            item_data["cost_price"][day_index],
        )
        records.append(record)

    merged_array = np.array(records, dtype=dtype)
    return merged_array


if __name__ == "__main__":
    # # 合并7月1日的数据（索引为0）
    # july_1_data = merge_daily_data(0)
    # tool.get_csv(july_1_data, "problem3_result")
    # data = july_1_data
    # addition = (data["sales_price"] - data["cost_price"]) / data["cost_price"]

    # # 将 addition 列添加到结构化数组中
    # data_with_addition = np.zeros(
    #     data.shape, dtype=data.dtype.descr + [("addition", "f4")]
    # )

    # # 复制原有数据
    # for name in data.dtype.names:
    #     data_with_addition[name] = data[name]

    # # 添加 addition 列的数据
    # data_with_addition["addition"] = addition

    # # 输出结果
    # print(data_with_addition)
    # tool.get_csv(data_with_addition, "problem3_result")
    data = tool.get_np("focus_product_stat.csv")
    pro_list = mapping.focus_products
    result = 0
    for i in pro_list:
        result += data["demand_index"][data["code"] == i]
    print(result)
